package net.xdclass.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.xdclass.controller.req.*;
import net.xdclass.dto.AccountFileDTO;
import net.xdclass.dto.FileChunkDTO;
import net.xdclass.dto.FolderTreeNodeDTO;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.service.AccountFileService;
import net.xdclass.service.FileChunkService;
import net.xdclass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 姜阿木
 * @date 2025/6/19 19:46
 */

@Tag(name = "用户文件管理", description = "处理用户的文件列表查询、创建文件夹及文件重命名等操作")
@RestController
@RequestMapping("/api/file/v1")
public class AccountFileController {

    @Autowired
    private AccountFileService accountFileService;

    @Autowired
    private FileChunkService fileChunkService;

    /**
     * 查询文件列表
     */
    @GetMapping("list")
    @Operation(summary = "获取文件列表", description = "根据父文件夹ID查询当前用户的文件列表")
    public JsonData list(
            @Parameter(name = "parent_id", description = "父文件夹ID，用于查询子文件列表", required = true)
            @RequestParam(value = "parent_id") Long parentId) {
        Long accountId = LoginInterceptor.threadLocal.get().getId();
        List<AccountFileDTO> list = accountFileService.list(accountId, parentId);
        return JsonData.buildSuccess(list);
    }

    /**
     * 创建文件夹
     */
    @PostMapping("create_folder")
    @Operation(summary = "创建文件夹", description = "允许用户创建一个新的文件夹")
    public JsonData createFolder(
            @Parameter(name = "FolderCreateReq", description = "包含文件夹名称和相关信息的请求体", required = true)
            @RequestBody FolderCreateReq req) {
        Long accountId = LoginInterceptor.threadLocal.get().getId();
        req.setAccountId(accountId);
        accountFileService.createFolder(req);
        return JsonData.buildSuccess();
    }

    /**
     * 文件重命名
     */
    @PostMapping("rename_file")
    @Operation(summary = "重命名文件", description = "允许用户对指定文件进行重命名操作")
    public JsonData renameFile(
            @Parameter(name = "FileUpdateReq", description = "包含文件ID和新文件名的请求体", required = true)
            @RequestBody FileUpdateReq req) {
        Long accountId = LoginInterceptor.threadLocal.get().getId();
        req.setAccountId(accountId);
        accountFileService.renameFile(req);
        return JsonData.buildSuccess();
    }

    /**
     * 获取文件树
     */
    @GetMapping("/folder/tree")
    @Operation(summary = "获取文件树", description = "获取当前用户的文件树结构")
    public JsonData folderTree() {
        Long accountId = LoginInterceptor.threadLocal.get().getId();
        List<FolderTreeNodeDTO> list = accountFileService.folderTree(accountId);
        return JsonData.buildSuccess(list);
    }

    /**
     * 小文件上传
     */
    @PostMapping("upload")
    @Operation(summary = "上传文件", description = "用户上传文件")
    public JsonData upload(FileUploadReq req){
        Long accountId = LoginInterceptor.threadLocal.get().getId();
        req.setAccountId(accountId);
        accountFileService.fileUpload(req);
        return JsonData.buildSuccess();
    }

    /**
     * 文件批量移动
     */
    @PostMapping("move_batch")
    @Operation(summary = "批量移动文件", description = "批量移动文件到指定文件夹")
    public JsonData moveBatch(@Parameter(description = "包含文件ID和目标文件夹ID的请求体", required = true)
                                  @RequestBody FileBatchReq req){
        req.setAccountId(LoginInterceptor.threadLocal.get().getId());
        accountFileService.moveBatch(req);
        return JsonData.buildSuccess();
    }

    /**
     * 文件批量删除
     */
    @PostMapping("del_batch")
    @Operation(summary = "批量删除文件", description = "批量删除文件")
    public JsonData delBatch(@Parameter(description = "包含文件ID的请求体", required = true)
                                 @RequestBody FileDelReq req){
        req.setAccountId(LoginInterceptor.threadLocal.get().getId());
        accountFileService.delBatch(req);
        return JsonData.buildSuccess();

    }

    /**
     * 文件批量复制
     */
    @PostMapping("copy_batch")
    @Operation(summary = "批量复制文件", description = "批量复制文件到指定文件夹")
    public JsonData copyBatch(@Parameter(description = "包含文件ID和目标文件夹ID的请求体", required = true)
                                  @RequestBody FileBatchReq req){
        req.setAccountId(LoginInterceptor.threadLocal.get().getId());
        accountFileService.copyBatch(req);
        return JsonData.buildSuccess();
    }

    /**
     * 文件妙传接口
     */
    @PostMapping("second_upload")
    @Operation(summary = "秒传接口", description = "用户秒传接口")
    public JsonData secondUpload(@Parameter(description = "包含文件信息、文件名和父文件夹ID的请求体", required = true)
                                     @RequestBody FileSecondUploadReq req){
        req.setAccountId(LoginInterceptor.threadLocal.get().getId());
        boolean result =accountFileService.secondUpload(req);
        return JsonData.buildSuccess(result);
    }

    /**
     * 1-创建分片上传任务
     */
    @PostMapping("init_file_chunk_task")
    @Operation(summary = "初始化分片上传任务", description = "初始化分片上传任务")
    public JsonData initFileChunkTask(@Parameter(description = "包含文件名和父文件夹ID的请求体", required = true)
                                          @RequestBody FileChunkInitTaskReq req){
        req.setAccountId(LoginInterceptor.threadLocal.get().getId());
        FileChunkDTO fileChunkDTO = fileChunkService.initFileChunkTask(req);
        return JsonData.buildSuccess(fileChunkDTO);
    }

    /**
     * 2-获取分片上传地址返回minio临时签名地址
     */
    @GetMapping("/get_file_chunk_upload_url/{identifier}/{partNumber}")
    @Operation(summary = "获取临时分片上传地址", description = "获取临时分片上传地址")
    public JsonData getFileChunkUploadUrl(@PathVariable("identifier") String identifier
            , @PathVariable("partNumber") int partNumber){
        Long accountId = LoginInterceptor.threadLocal.get().getId();
        String url = fileChunkService.genPreSignUploadUrl(accountId, identifier, partNumber);
        return JsonData.buildSuccess(url);
    }

    /**
     * 3-合并分片
     */
    @PostMapping("merge_file_chunk")
    @Operation(summary = "合并分片", description = "合并分片")
    public JsonData mergeFileChunk(@Parameter(description = "合并分片参数", required = true)
                                       @RequestBody FileChunkMergeReq req){
        req.setAccountId(LoginInterceptor.threadLocal.get().getId());
        fileChunkService.mergeFileChunk(req);
        return JsonData.buildSuccess();
    }

    /**
     * 查询分片上传进度
     */
    @GetMapping("/chunk_upload_progress/{identifier}")
    @Operation(summary = "查询分片上传进度", description = "查询分片上传进度")
    public JsonData chunkUploadProgress(@PathVariable("identifier") String identifier){
        Long accountId = LoginInterceptor.threadLocal.get().getId();
        FileChunkDTO fileChunkDTO= fileChunkService.listFileChunk(accountId, identifier);
        return JsonData.buildSuccess(fileChunkDTO);
    }

}










